package com.sizu.web;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.sizu.common.dto.OverflowListDto;
import com.sizu.common.vo.ResultEntity;
import com.sizu.entity.TOverflowList;
import com.sizu.entity.TOverflowListGoods;
import com.sizu.service.ITOverflowListGoodsService;
import com.sizu.service.ITOverflowListService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-28
 */
@RestController
@RequestMapping("/inventory/tOverflowList")
public class TOverflowListController {

    @Autowired
    private ITOverflowListGoodsService iTOverflowListGoodsService;

    @Autowired
    private ITOverflowListService tTOverflowListService;

    /**
     * 返回报溢单随机编号
     * @return
     */
    @RequestMapping("/number")
    public ResultEntity getOrderNumber(){
        return ResultEntity.ok("获取成功","BY"+System.currentTimeMillis());
    }

    //做报溢单和报溢单详情添加的方法
    @RequestMapping("/overflowListAdd")
    public ResultEntity overflowListAdd(OverflowListDto overflowListDto){
        TOverflowList tOverflowList = new TOverflowList();
        BeanUtils.copyProperties(overflowListDto,tOverflowList);
        tTOverflowListService.insert(tOverflowList);//向订单表里添加数据
        //获取新添加进去的id
        Wrapper<TOverflowList> wrapper = new EntityWrapper<TOverflowList>(tOverflowList);
        TOverflowList tOverflowList1 = tTOverflowListService.selectOne(wrapper);
        if(null!=overflowListDto.getOverflowListGoodsJSON()){
            List<TOverflowListGoods> tOverflowListGoods = JSON.parseArray(overflowListDto.getOverflowListGoodsJSON(), TOverflowListGoods.class);
            for (TOverflowListGoods tOverflowListGood : tOverflowListGoods) {
                tOverflowListGood.setOverflowListId(tOverflowList1.getId());
                Wrapper<TOverflowListGoods> wrapper1 = new EntityWrapper<TOverflowListGoods>();
                wrapper1.eq("name",tOverflowListGood.getName());
                TOverflowListGoods tOverflowListGoods1 = iTOverflowListGoodsService.selectOne(wrapper1);
                if(null!=tOverflowListGoods1){
                    iTOverflowListGoodsService.updateById(tOverflowListGood);
                }
                iTOverflowListGoodsService.insert(tOverflowListGood);
            }
        }
        return ResultEntity.ok();
    }
}
